package retrofit.callback;


import com.wanpinghui.wphlibrary.WPHLibrary;
import com.yunshl.yunshllibrary.storage.LogManager;
import com.yunshl.yunshllibrary.utils.TextUtil;

import retrofit.ToKenUtil;
import rx.functions.Action1;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/6/15
 ***/

public class YunShlAction<T> implements Action1<T> {
    protected YRequestCallback callback;
    private String message;

    public YunShlAction(YRequestCallback callback) {
        this.callback = callback;
    }

    public YunShlAction(YRequestCallback callback, String saveToLogMessage) {
        this.callback = callback;
        this.message = saveToLogMessage;
    }

    @Override
    public void call(T o) {
        if (TextUtil.isNotEmpty(message)) {
            LogManager.getInstance().w("YunShlAction", message+" : "+((Throwable)o).getMessage());
        }
       if (o instanceof NetworkException) {
           if (((NetworkException)o).getType() == FailOrExceptionType.NO_LOGIN) {
               ToKenUtil.deleteToken();
               WPHLibrary.getLibrary().sendRequestFial(FailOrExceptionType.NO_LOGIN, ((NetworkException)o).getCode());
               if (callback != null) {
                   callback.onFailed(FailOrExceptionType.NO_LOGIN, "");
               }
           }else {
               if (callback != null) {
                   callback.onFailed(0, ((NetworkException) o).getMessage());
               }
           }
       }else {
           if (callback != null) {
               callback.onException((Throwable) o);
           }
       }
    }

}
